package com.myweb.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 
 * @TODO 日期处理工具类
 * @author roye
 * @date May 10, 2013 8:17:13 PM
 * @version V1.0
 */
public class DateUtil {
	

    /**
     * 返回dateFormat格式的时间字符串
     * 
     * @param date
     * @param dateFormat
     *            e.g:yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String getDateFormat(Date date, String dateFormat) {
        DateFormat df = new SimpleDateFormat(dateFormat);
        return df.format(date);
    }

    /**
     * 根据dateFormat格式构建时间
     * 
     * @param dateStrig
     * @param dateFormat
     *            e.g:yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static Date getDate(String dateStrig, String dateFormat) {
        DateFormat df = new SimpleDateFormat(dateFormat);
        try {
            return df.parse(dateStrig);
        } catch (ParseException e) {
            return null;
        }
    }
    
    /**
     * 时间运算
     * 
     * @param date
     * @param type
     *            e.g:Calendar.DATE
     * @param value
     * @return
     */
    public Date addDate(Date date, int type, int value) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(type, value);
        return calendar.getTime();
    }

    /**
     * 返回两个日期相差的毫秒数
     * 
     * @param startDate
     * @param endDate
     * @return
     */
    public long deltaTMillisecond(Date startDate, Date endDate) {
        return endDate.getTime() - startDate.getTime();
    }

    /**
     * 返回两个日期相差的秒数
     * 
     * @param startDate
     * @param endDate
     * @return
     */
    public long deltaTSecond(Date startDate, Date endDate) {
        return deltaTMillisecond(startDate, endDate) / 1000;
    }

    /**
     * 返回两个日期相差的分钟数
     * 
     * @param startDate
     * @param endDate
     * @return
     */
    public long deltaTMinute(Date startDate, Date endDate) {
        return deltaTSecond(startDate, endDate) / 60;
    }

    /**
     * 返回两个日期相差的小时数
     * 
     * @param startDate
     * @param endDate
     * @return
     */
    public long deltaTHour(Date startDate, Date endDate) {
        return deltaTMinute(startDate, endDate) / 60;
    }

    /**
     * 返回两个日期相差的天数
     * 
     * @param startDate
     * @param endDate
     * @return
     */
    public long deltaTDay(Date startDate, Date endDate) {
        return deltaTHour(startDate, endDate) / 24;
    }

    /**
     * 计算两个日期之间的工作日，不支持自定义假日。
     * 
     * @param startDate
     * @param endDate
     * @return
     */
    public long deltaTWorkDay(Date startDate, Date endDate) {
        long days = deltaTDay(startDate, endDate);
        int num=1;
        if(days<0){
            num=-num;
        }
        long workdays = days;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        while(days!=0){
            if (calendar.get(Calendar.DAY_OF_WEEK) == 1
                    || calendar.get(Calendar.DAY_OF_WEEK) == 7) {
                workdays=workdays-num;;
            }
            calendar.add(Calendar.DATE, num);
            days=days-num;
        }
        return workdays;
    }

    /**
     * 计算两个日期之间的工作日，支持自定义假日。
     * 
     * @param startDate
     * @param endDate
     * @param holidays
     * @return
     */
    public long deltaTWorkDay(Date startDate, Date endDate,
            Date[] holidays) {
        long days = deltaTDay(startDate, endDate);
        int num=1;
        if(days<0){
            num=-num;
        }
        long workdays = days;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        Calendar holidayCalendar = Calendar.getInstance();
        while(days!=0){
            //星期六、日
            if (calendar.get(Calendar.DAY_OF_WEEK) == 1
                    || calendar.get(Calendar.DAY_OF_WEEK) == 7) {
                workdays=workdays-num;
            }
            for (int j = 0; j < holidays.length; j++) {
                holidayCalendar.setTime(holidays[j]);
                //自定义假日，并且不是星期六、日
                if ((calendar.get(Calendar.YEAR) == holidayCalendar
                        .get(Calendar.YEAR)
                        && calendar.get(Calendar.MONTH) == holidayCalendar
                                .get(Calendar.MONTH) && calendar
                        .get(Calendar.DAY_OF_MONTH) == holidayCalendar
                        .get(Calendar.DAY_OF_MONTH))
                        && !(holidayCalendar.get(Calendar.DAY_OF_WEEK) == 1 || holidayCalendar
                                .get(Calendar.DAY_OF_WEEK) == 7)) {
                    workdays=workdays-num;
                }
            }
            calendar.add(Calendar.DATE, num);
            days=days-num;
        }
        return workdays;
    }

    /**
     * 当前日期+days个工作日=日期，不支持自定义假日
     * 
     * @param date
     * @param days
     * @return
     */
    public Date addWorkDay(Date date, long days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int num = 1;
        if (days < 0) {
            num = -num;
        }
        while (days != 0) {
            if (calendar.get(Calendar.DAY_OF_WEEK) != 1
                    && calendar.get(Calendar.DAY_OF_WEEK) != 7) {
                System.out
                        .println(getDateFormat(calendar.getTime(), "yyyyMMdd"));
                days = days - num;
            }
            calendar.add(Calendar.DATE, num);
        }
        return calendar.getTime();
    }

    /**
     * 当前日期+days个工作日=日期，支持自定义假日
     * 
     * @param date
     * @param days
     * @param holidays
     * @return
     */
    public Date addWorkDay(Date date, long days, Date[] holidays) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        Calendar holidayCalendar = Calendar.getInstance();
        int num = 1;
        if (days < 0) {
            num = -num;
        }
        while (days != 0) {
            // 不是星期六、日
            System.out.println(getDateFormat(calendar.getTime(), "yyyyMMdd"));
            if (calendar.get(Calendar.DAY_OF_WEEK) != 1
                    && calendar.get(Calendar.DAY_OF_WEEK) != 7) {
                days = days - num;
                System.out.println(days);
                for (int j = 0; j < holidays.length; j++) {
                    holidayCalendar.setTime(holidays[j]);
                    //不是星期六、日，但是是自定义假日
                    if ((calendar.get(Calendar.YEAR) == holidayCalendar
                            .get(Calendar.YEAR)
                            && calendar.get(Calendar.MONTH) == holidayCalendar
                                    .get(Calendar.MONTH) && calendar
                            .get(Calendar.DAY_OF_MONTH) == holidayCalendar
                            .get(Calendar.DAY_OF_MONTH))) {
                        days = days + num;
                        break;
                    }
                }
            }
            calendar.add(Calendar.DATE, num);
        }
        return calendar.getTime();
    }

    /**
     * 取一个季度开始时间
     * 
     * @param date
     * @return
     */
    public Date getFirstDateOfQuarter(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH);
        // 一个季度的末月
        if ((month + 1) % 3 == 0) {
            calendar.set(calendar.get(Calendar.YEAR), (calendar
                    .get(Calendar.MONTH) - 2), calendar
                    .getActualMinimum(Calendar.DAY_OF_MONTH), calendar
                    .getActualMinimum(Calendar.HOUR_OF_DAY), calendar
                    .getActualMinimum(Calendar.MINUTE), calendar
                    .getActualMinimum(Calendar.SECOND));
        }
        // 季度中间月
        else if ((month + 1) % 3 == 2) {
            calendar.set(calendar.get(Calendar.YEAR), (calendar
                    .get(Calendar.MONTH) - 1), calendar
                    .getActualMinimum(Calendar.DAY_OF_MONTH), calendar
                    .getActualMinimum(Calendar.HOUR_OF_DAY), calendar
                    .getActualMinimum(Calendar.MINUTE), calendar
                    .getActualMinimum(Calendar.SECOND));
        } else {
            calendar.set(calendar.get(Calendar.YEAR), calendar
                    .get(Calendar.MONTH), calendar
                    .getActualMinimum(Calendar.DAY_OF_MONTH), calendar
                    .getActualMinimum(Calendar.HOUR_OF_DAY), calendar
                    .getActualMinimum(Calendar.MINUTE), calendar
                    .getActualMinimum(Calendar.SECOND));
        }
        return calendar.getTime();
    }

    /**
     * 取一个季度结束时间
     * 
     * @param date
     * @return
     */
    public Date getLastDateOfQuarter(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH);
        // 季度始月
        if ((month + 1) % 3 == 1) {
            calendar.set(calendar.get(Calendar.YEAR), (calendar
                    .get(Calendar.MONTH) + 2), calendar
                    .getActualMaximum(Calendar.DAY_OF_MONTH), calendar
                    .getActualMaximum(Calendar.HOUR_OF_DAY), calendar
                    .getActualMaximum(Calendar.MINUTE), calendar
                    .getActualMaximum(Calendar.SECOND));
        }
        // 季度中间月
        else if ((month + 1) % 3 == 2) {
            calendar.set(calendar.get(Calendar.YEAR), (calendar
                    .get(Calendar.MONTH) + 1), calendar
                    .getActualMaximum(Calendar.DAY_OF_MONTH), calendar
                    .getActualMaximum(Calendar.HOUR_OF_DAY), calendar
                    .getActualMaximum(Calendar.MINUTE), calendar
                    .getActualMaximum(Calendar.SECOND));
        } else {
            calendar.set(calendar.get(Calendar.YEAR), calendar
                    .get(Calendar.MONTH), calendar
                    .getActualMaximum(Calendar.DAY_OF_MONTH), calendar
                    .getActualMaximum(Calendar.HOUR_OF_DAY), calendar
                    .getActualMaximum(Calendar.MINUTE), calendar
                    .getActualMaximum(Calendar.SECOND));
        }
        return calendar.getTime();
    }

    /**
     * 取一个月的开始时间
     * 
     * @param date
     * @return
     */
    public Date getFirstDateOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                calendar.getActualMinimum(Calendar.DAY_OF_MONTH), calendar
                        .getActualMinimum(Calendar.HOUR_OF_DAY), calendar
                        .getActualMinimum(Calendar.MINUTE), calendar
                        .getActualMinimum(Calendar.SECOND));
        return calendar.getTime();
    }

    /**
     * 取一个月的结束时间
     * 
     * @param date
     * @return
     */
    public Date getLastDateOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                calendar.getActualMaximum(Calendar.DAY_OF_MONTH), calendar
                        .getActualMaximum(Calendar.HOUR_OF_DAY), calendar
                        .getActualMaximum(Calendar.MINUTE), calendar
                        .getActualMaximum(Calendar.SECOND));
        return calendar.getTime();
    }

    /**
     * 取一个星期的开始时间
     * 
     * @param date
     * @return
     */
    public Date getFirstDateOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_WEEK, calendar
                .getActualMinimum(Calendar.DAY_OF_WEEK));
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), calendar
                        .getActualMinimum(Calendar.HOUR_OF_DAY), calendar
                        .getActualMinimum(Calendar.MINUTE), calendar
                        .getActualMinimum(Calendar.SECOND));
        return calendar.getTime();
    }

    /**
     * 取一个星期的结束时间
     * 
     * @param date
     * @return
     */
    public Date getLastDayOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_WEEK, calendar
                .getActualMaximum(Calendar.DAY_OF_WEEK));
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), calendar
                        .getActualMaximum(Calendar.HOUR_OF_DAY), calendar
                        .getActualMaximum(Calendar.MINUTE), calendar
                        .getActualMaximum(Calendar.SECOND));
        return calendar.getTime();
    }


}
